package com.tanhua.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.dto.FreezeDto;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.service.UserFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author wjs
 * @version 1.0
 * @date 2022/1/4 0004 下午 3:10
 */
@Service
public class UserFreezeServiceImpl implements UserFreezeService {

    @Autowired
    private RedisTemplate redisTemplate;

    public void checkUserStatus(Integer state, Long userId) {
        String value = (String) redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
//        String value = (String) redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId + state);
        if (!StringUtils.isEmpty(value)) {
            FreezeDto dto = JSON.parseObject(value, FreezeDto.class);
            Integer freezingRange = dto.getFreezingRange();//1 2  3
            if (freezingRange == state) {
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结！").build());
            }
        }
    }

}
